<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Message extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $model = null;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new \app\api\model\Message();
    }


    public function getList()
    {
        $notice = $this->model->where(['type'=>1])->select();
        $message = $this->model->where(['type'=>2])->select();
        $notice_not_read = $this->model->where(['type'=>1,'is_read'=>1])->select();
        $message_not_read = $this->model->where(['type'=>2,'is_read'=>1])->select();
        $data = [
            'notice'=>$notice,
            'message'=>$message,
            'notice_not_read'=>count($notice_not_read),
            'message_not_read'=>count($message_not_read)
        ];
        $this->success('',$data);
    }

    public function getInfo(){
        $id = $this->request->param('id');
        if (empty($id)) $this->error('参数错误');

        $info = $this->model->where(['id'=>$id])->find();
        if(empty($info)) $this->error('信息不存在');
        $info->save(['is_read'=>2]);
        $this->success('',$info);
    }
}